/**
 * Created by yangh on 2016/09/03.
 */
var main = {};
var myScroll;
(function ($) {
    main.init = function () {
    	 document.addEventListener('touchmove', function (e) {
             e.preventDefault();
         }, false);
    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
        if (this.y < main.max - 80) {
            $("#tip").hide();
            $("#loading").show();
            var maxId=$("#goodsShow li:last").val();
            var html = "";
            $.ajax({
            	type:"post",
	        	url:path+"/commodity/search",
	        	data:{
	        		"search":searchCondition,
	        		"marketId":marketId,
	        		"Id":maxId	
	        	},
        		success:function(resultjson){	
        			if('000' == resultjson.code){
        				var html = "";
        				if(resultjson.result.length>0){
        					for (var i = 0; i < resultjson.result.length; i++) {
        						 html += '<li value="'+resultjson.result[i].goods.id+'">';
	        					 html += '<a href='+path+'/commodity/goodsinfo?goodsId='+resultjson.result[i].goods.id+'>';
	        					 html += '<div class="goods-pic">';
	        					 html += '<img src="'+resultjson.result[i].goods.picurl+'"></div>';
	        					 html += '<h3>'+resultjson.result[i].goods.name+'</h3>';
	        					 html += '<div class="score">';
								  if(resultjson.result[i].score==0){
								 	html += '<span class="scoreCon" data-score="10"> </span>';
								 }else{
									html += '<span class="scoreCon" data-score="'+resultjson.result[i].score+'"> </span>';
								 }
	        					 html += ' </div>';
	        					 html += ' <div class="price">￥'+resultjson.result[i].goods.price+'元</div>';
	        					 html += ' </a> ';
        					}	
        					$("#goodsShow").append(html);
        					
        					if(resultjson.result.length==0){
            					$("#tip").text("暂无商品");
            				}else if(resultjson.result.length<20){
                				$("#loading").hide();
    			  	            $("#tip").hide();
            				}else if(resultjson.result.length>=20){
     	  		            	$("#loading").hide();
     			  	            $("#tip").show();
     			  	            canLoad=true;
     	  		            }
        					  /* 评分 */
    		   				 score($(".scoreCon"));
    		   				myScroll.refresh();
        		            main.max = myScroll.maxScrollY;
        		            console.log(main.max);
        				}
        				
        			}
        		}
        	})
           
        }
    }

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
        myScroll = new IScroll(".detail", {probeType: 3, mouseWheel: true, click: true, preventDefault: false});

        main.max = myScroll.maxScrollY;
        /*console.log(main.max);*/

        myScroll.on("scroll", move);
    }
})(jQuery);

main.init();